﻿// ===============================================================================
//  Copyright © ymind.net. All rights reserved .
//  官方网站：http://ymind.net/
//  版权所有：彦铭工作室 老陈出品
// ===============================================================================

using System;
using System.Web;

namespace Lcsoft.UrlRewriter.Actions
{
	/// <summary>
	/// 提供设置 Cookie 功能的地址重写动作。
	/// </summary>
	public class SetCookieAction : IRewriteAction
	{
		/// <summary>
		/// 初始化 <see cref="SetCookieAction"/> 类的新实例。
		/// </summary>
		/// <param name="cookieName">Cookie 名称。</param>
		/// <param name="cookieValue">Cookie 的值。</param>
		public SetCookieAction(string cookieName, string cookieValue)
		{
			if (cookieName == null) throw new ArgumentNullException("cookieName");
			if (cookieValue == null) throw new ArgumentNullException("cookieValue");

			this.Name = cookieName;
			this.Value = cookieValue;
		}

		/// <summary>
		/// 获取 Cookie 名称。
		/// </summary>
		public string Name { get; private set; }

		/// <summary>
		/// 获取 Cookie 的值。
		/// </summary>
		public string Value { get; private set; }

		#region IRewriteAction Members

		/// <summary>
		/// 执行动作。
		/// </summary>
		/// <param name="context"><see cref="RewriteContext"/> 对象的实例。</param>
		/// <returns>返回 <see cref="RewriteProcessing"/> 枚举值之一。</returns>
		public RewriteProcessing Execute(RewriteContext context)
		{
			if (context == null) throw new ArgumentNullException("context");

			var cookie = new HttpCookie(this.Name, this.Value);
			context.Cookies.Add(cookie);

			return RewriteProcessing.ContinueProcessing;
		}

		#endregion
	}
}
